<?php

namespace Admin\Controller;

class FinishedProductController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("FinishedProduct");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["fp.enterprise_code"] = $user["enterprise_code"];
        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $this->assign("user", $user);

        $this->display();

    }

    public function add() {
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $agentIdArr = D('Agent')
            ->where(array('enterprise_code'=>$user['enterprise_code']))
            ->select();

        $list = array();
        foreach ($agentIdArr as $agent){
                $list[] = array("agent_id" => $agent["agent_id"], "agent_name" => $agent["agent_name"]);
        }
        $this->assign("list", $list);
        $this->assign("user", $user);
        $this->display("add");
    }

    public function edit() {
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $id = $_REQUEST["id"];
        $info = D("FinishedProduct")->queryById($id);
        $agentIdArr = D('Agent')
            ->where(array('enterprise_code'=>$user['enterprise_code']))
            ->select();
        $userAgentIdArr = explode(",", $info["agent_id"]);
        $priceArr = explode(",", $info["agent_price"]);
        $list = array();
        foreach ($agentIdArr as $agent){
            if(in_array($agent["agent_id"], $userAgentIdArr)){
                $key = array_search($agent['agent_id'], $userAgentIdArr);
                $list[] = array("agent_id" => $agent["agent_id"], "agent_name" => $agent["agent_name"], "agent_price" => $priceArr[$key]);
            }else{
                $list[] = array("agent_id" => $agent["agent_id"], "agent_name" => $agent["agent_name"]);
            }

        }
        $this->assign("list", $list);
        $this->assign("user", $user);
        $this->assign("info", $info);
        $this->display("edit");
    }

    public function addDesc() {
        $productId = $_REQUEST["id"];
        $this->assign("productId", $productId);
        $this->display("addDesc");
    }

    public function remove() {

        $id = $_REQUEST['id'];

        D("FinishedProduct")->deleteById($id);

        $this->success('删除成功!', U("finishedProduct/index"));

    }

     /**
     * 添加数据
     */
    public function addPost() {

        $data["class_id"] = $_REQUEST["classId"];
        $data["code"] = $_REQUEST["code"];
        $data["name"] = $_REQUEST["productName"];
        $data["number"] = $_REQUEST["number"];
        $data["function"] = $_REQUEST["function"];
        $data["indication"] = $_REQUEST["indication"];
        $data["size"] = $_REQUEST["size"];
        $data["size_detail"] = $_REQUEST["size_detail"];
        $data["size_comment"] = $_REQUEST["size_comment"];
        $data["unit"] = $_REQUEST["unit"];
        $data["price"] = $_REQUEST["price"];
        $data["discount"] = $_REQUEST["discount"];
        $data["supplier_id"] = $_REQUEST["supplier_id"];
        if(isNullOrEmpty($_REQUEST["imageUrl"])){
            $data["image_url"] = "http://xwk-file.oss-cn-qingdao.aliyuncs.com/Home/FinishedProduct.png";
        }else{
            $data["image_url"] = $_REQUEST["imageUrl"];
        }

        $data["agent_id"] = $_REQUEST["agentId"];
        $data["agent_price"] = $_REQUEST["agentPrice"];
        if(isNullOrEmpty($data["agent_id"])){
            $data["has_agent"] = "";
        }else{
            $data["has_agent"] = 1;
        }
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["enterprise_code"] = $user["enterprise_code"];

        $this->currentModel->add($data);

        $this->success("添加成功", U("finishedProduct/index"));

    }

    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];

        $data = $this->currentModel->queryById($id);
        $data["class_id"] = $_REQUEST["classId"];
        $data["code"] = $_REQUEST["code"];
        $data["name"] = $_REQUEST["productName"];
        $data["number"] = $_REQUEST["number"];
        $data["function"] = $_REQUEST["function"];
        $data["indication"] = $_REQUEST["indication"];
        $data["size"] = $_REQUEST["size"];
        $data["size_detail"] = $_REQUEST["size_detail"];
        $data["size_comment"] = $_REQUEST["size_comment"];
        $data["unit"] = $_REQUEST["unit"];
        $data["price"] = $_REQUEST["price"];
        $data["discount"] = $_REQUEST["discount"];
        $data["supplier_id"] = $_REQUEST["supplier_id"];
        if(isNullOrEmpty($_REQUEST["imageUrl"])){
            $data["image_url"] = "http://xwk-file.oss-cn-qingdao.aliyuncs.com/Home/FinishedProduct.png";
        }else{
            $data["image_url"] = $_REQUEST["imageUrl"];
        }
        $data["agent_id"] = $_REQUEST["agentId"];
        $data["agent_price"] = $_REQUEST["agentPrice"];
        if(isNullOrEmpty($data["agent_id"])){
            $data["has_agent"] = "";
        }else{
            $data["has_agent"] = 1;
        }

        $this->currentModel->updateInfo($data);

        $this->success("更新成功", U("finishedProduct/index"));

    }

    public function classlist() {
        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["c.enterprise_code"] = $user["enterprise_code"];
        //分页
        $count = D("Class")->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = D("Class")->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);

        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display("FinishedProduct/classlist");

    }
    public function removeClass() {

        $id = $_REQUEST['id'];
        D("Class")->deleteById($id);

        $this->success('删除成功!', U("FinishedProduct/classlist"));

    }

    public function addClass() {
        $this->display("FinishedProduct/addClass");
    }

    /**
     * 添加数据
     */
    public function addClassPost() {

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["enterprise_code"] = $user["enterprise_code"];
        $data["update_id"] = $user["employee_id"];

        $data["class_name"] = $_REQUEST["class_name"];
        $data["create_time"] = getCurrentDate();
        $data["update_time"] = getCurrentDate();

        D("Class")->add($data);

        $this->success("添加成功", U("FinishedProduct/classlist"));

    }

    public function editClass() {
        $id = $_REQUEST["id"];
        $info = D("Class")->queryById($id);
        $this->assign("info", $info);
        $this->display("FinishedProduct/editClass");
    }
    /**
     * 更新数据
     */
    public function editClassPost() {

        $id = $_REQUEST["id"];
        $data = D("Class")->queryById($id);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
//        $data["enterprise_code"] = $user["enterprise_code"];
        $data["update_id"] = $user["employee_id"];

        $data["class_name"] = $_REQUEST["class_name"];
        $data["update_time"] = getCurrentDate();

        D("Class")->updateInfo($data);

        $this->success("更新成功", U("FinishedProduct/classlist"));

    }
    public function agentlist() {
        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["a.enterprise_code"] = $user["enterprise_code"];
        //分页
        $count = D("Agent")->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = D("Agent")->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);

        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display("FinishedProduct/agentlist");

    }
    public function removeAgent() {

        $id = $_REQUEST['id'];
        D("Agent")->deleteById($id);

        $this->success('删除成功!', U("FinishedProduct/agentlist"));

    }

    public function addAgent() {
        $this->display("FinishedProduct/addAgent");
    }

    /**
     * 添加数据
     */
    public function addAgentPost() {

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["enterprise_code"] = $user["enterprise_code"];
        $data["update_id"] = $user["employee_id"];
        $data["create_time"] = getCurrentDate();
        $data["update_time"] = getCurrentDate();
        $data["agent_name"] = $_REQUEST["agent_name"];
        $data["agent_contact"] = $_REQUEST["agent_contact"];
        $data["agent_tel"] = $_REQUEST["agent_tel"];
        $data["agent_address"] = $_REQUEST["agent_address"];
        $data["agent_invoice_name"] = $_REQUEST["agent_invoice_name"];
        $data["agent_invoice_number"] = $_REQUEST["agent_invoice_number"];
        $data["agent_invoice_detail"] = $_REQUEST["agent_invoice_detail"];
        $data["need_patient"] = $_REQUEST["need_patient"];
        $data["delivery_type"] = $_REQUEST["delivery_type"];
        $data["payment_type"] = $_REQUEST["payment_type"];
        $data["need_invoice"] = $_REQUEST["need_invoice"];
        $data["special_agent"] = $_REQUEST["special_agent"];

        D("Agent")->add($data);

        $this->success("添加成功", U("FinishedProduct/agentlist"));

    }

    public function editAgent() {
        $id = $_REQUEST["id"];
        $info = D("Agent")->queryById($id);
        $this->assign("info", $info);
        $this->display("FinishedProduct/editAgent");
    }
    /**
     * 更新数据
     */
    public function editAgentPost() {

        $id = $_REQUEST["id"];
        $data = D("Agent")->queryById($id);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
//        $data["enterprise_code"] = $user["enterprise_code"];
        $data["update_id"] = $user["employee_id"];
        $data["update_time"] = getCurrentDate();
        $data["agent_name"] = $_REQUEST["agent_name"];
        $data["agent_contact"] = $_REQUEST["agent_contact"];
        $data["agent_tel"] = $_REQUEST["agent_tel"];
        $data["agent_address"] = $_REQUEST["agent_address"];
        $data["agent_invoice_name"] = $_REQUEST["agent_invoice_name"];
        $data["agent_invoice_number"] = $_REQUEST["agent_invoice_number"];
        $data["agent_invoice_detail"] = $_REQUEST["agent_invoice_detail"];
        $data["need_patient"] = $_REQUEST["need_patient"];
        $data["delivery_type"] = $_REQUEST["delivery_type"];
        $data["payment_type"] = $_REQUEST["payment_type"];
        $data["need_invoice"] = $_REQUEST["need_invoice"];
        $data["special_agent"] = $_REQUEST["special_agent"];

        D("Agent")->updateInfo($data);

        $this->success("更新成功", U("FinishedProduct/agentlist"));

    }
}